package com.wunong.smart.commission.domain.converter;

import com.wunong.smart.commission.client.dto.CommissionDTO;
import com.wunong.smart.commission.client.enums.CommissionStatusEnum;
import com.wunong.smart.commission.domain.model.CommissionDomain;
import com.wunong.smart.domain.platform.common.BizSource;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author create by zealot.zt
 */
public class CommissionDomainConverter {

    public static List<CommissionDTO> toDto(List<CommissionDomain> domains) {
        if (CollectionUtils.isEmpty(domains)) {
            return null;
        }

        return domains.stream().map(CommissionDomainConverter::toDto)
                .collect(Collectors.toList());
    }

    public static CommissionDTO toDto(CommissionDomain domain) {
        if (domain == null) {
            return null;
        }

        CommissionDTO dto = CommissionDTO.of();
        dto.init(dto);

        dto.setUserId(domain.getUserId());
        dto.setUserRole(domain.getUserRole());

        BizSource source = domain.getSource();
        dto.setSource(source);

        dto.setCommissionType(domain.getCommissionType());
        dto.setStatus(domain.getStatus());

        CommissionStatusEnum statusEnum = domain.getStatusEnum();
        if (CommissionStatusEnum.CANCELED == statusEnum) {
            dto.setCancelTime(domain.getGmtModified());
        } else if (CommissionStatusEnum.FINISHED == statusEnum) {
            dto.setFinishTime(domain.getFinishTime());
        }

        dto.setContent(domain.getContent());

        return dto;
    }

}
